#include "SuperClassFill.h"

void initStmt(struct StmtInfo* stmt) {
    stmt->pExprLeft=NULL;
    stmt->pExprMid=NULL;
    stmt->pExprRight=NULL;
    stmt->pIfStmt=NULL;
    stmt->pStmtBlock=NULL;
    stmt->pStmtLeft=NULL;
    stmt->pStmtNext=NULL;
    stmt->pStmtRight=NULL;
    stmt->pSwitchStmt=NULL;
    stmt->VarnameLeft=NULL;
    stmt->VarnameRight=NULL;
}


void initExpr(struct ExprInfo* expr) {
    expr->args=NULL;
    expr->arrElements=NULL; 
    expr->const_char=NULL;
    expr->funcname=NULL;
    expr->left=NULL;
    expr->mid=NULL;
    expr->right=NULL;
    expr->pNext=NULL;
    expr->varname=NULL;
    expr->fieldname=NULL;
    expr->localTableIndex=-1;
    expr->constTableIndex=-1;
}



void doFieldToSuperClass(struct ClassDefInfo* pSuper,struct ClassBodyElement* pBody) {
	if(pSuper->body->pFirst == NULL) {
		pSuper->body->pFirst = (struct ClassBodyElement*) malloc(sizeof(struct ClassBodyElement));
		pSuper->body->pFirst->pNext = NULL;
		pSuper->body->pLast = pSuper->body->pFirst;
	} else {
		pSuper->body->pLast->pNext = (struct ClassBodyElement*) malloc(sizeof(struct ClassBodyElement));
		pSuper->body->pLast = pSuper->body->pLast->pNext;
		pSuper->body->pLast->pNext = NULL;
	}	
		pSuper->body->pLast->aclev = pBody->aclev;
		pSuper->body->pLast->var = (char*) malloc(sizeof(char)*20);
		strcpy(pSuper->body->pLast->var, pBody->var);
		
		pSuper->body->pLast->pMethod = NULL;
}

void doMethodToSuperClass(struct ClassDefInfo* pSuper,struct ClassBodyElement* pBody) {
	if(pSuper->body->pFirst == NULL) {
		pSuper->body->pFirst = (struct ClassBodyElement*) malloc(sizeof(struct ClassBodyElement));
		pSuper->body->pFirst->pNext = NULL;
		pSuper->body->pLast = pSuper->body->pFirst;
	} else {
		pSuper->body->pLast->pNext = (struct ClassBodyElement*) malloc(sizeof(struct ClassBodyElement));
		pSuper->body->pLast = pSuper->body->pLast->pNext;
		pSuper->body->pLast->pNext = NULL;
	}	

	pSuper->body->pLast->aclev = pBody->aclev;
	pSuper->body->pLast->pMethod = (struct FuncDefInfo*) malloc(sizeof(struct FuncDefInfo));
	pSuper->body->pLast->pMethod->id = (char*) malloc(sizeof(char)*20);
	strcpy(pSuper->body->pLast->pMethod->id,pBody->pMethod->id);

	pSuper->body->pLast->pMethod->pStmtList = (struct StmtListInfo*) malloc(sizeof(struct StmtListInfo));
	pSuper->body->pLast->pMethod->pStmtList->pStmtFirst=NULL;
	pSuper->body->pLast->pMethod->pStmtList->pStmtLast = NULL;

	//struct StmtInfo* Stmt = (struct StmtInfo*) malloc(sizeof(struct StmtInfo));
	//initStmt(Stmt);

	//struct ExprInfo* Expr = (struct ExprInfo*) malloc(sizeof(struct ExprInfo));
	//initExpr(Expr);
	//Expr->type = charcnst;
	//Expr->const_char = (char*) malloc(sizeof(char)*30);
	//strcpy( Expr->const_char,"not supported yet");

	//Stmt = CreateSingleExprStmt(Expr, Echo);

	//pSuper->body->pLast->pMethod->pStmtList = CreateStmtList(Stmt);

    if(strcmp(pBody->pMethod->id, CONSTRUCT) != 0) {
		
		struct StmtInfo* Stmt_2 = (struct StmtInfo*) malloc(sizeof(struct StmtInfo));
		initStmt(Stmt_2);
		Stmt_2->type = Return;
		Stmt_2->pExprLeft = (struct ExprInfo*) malloc(sizeof(struct ExprInfo));
		initExpr(Stmt_2->pExprLeft);
		Stmt_2->pExprLeft->type = intcnst;
		Stmt_2->pExprLeft->const_int = 0;
		//pSuper->body->pLast->pMethod->pStmtList = AddToStmtList(pSuper->body->pLast->pMethod->pStmtList, Stmt_2);
		pSuper->body->pLast->pMethod->pStmtList = CreateStmtList(Stmt_2);
			
	} else {
		
		struct StmtInfo* Stmt_2 = (struct StmtInfo*) malloc(sizeof(struct StmtInfo));
		initStmt(Stmt_2);
		Stmt_2->type = Return;
		Stmt_2->pExprLeft = NULL;
		//pSuper->body->pLast->pMethod->pStmtList = AddToStmtList(pSuper->body->pLast->pMethod->pStmtList, Stmt_2);
		pSuper->body->pLast->pMethod->pStmtList = CreateStmtList(Stmt_2);
	}

	pSuper->body->pLast->pMethod->pVarList  = (struct VarListInfo*) malloc(sizeof(struct VarListInfo));
	if(pBody->pMethod->pVarList != NULL) {
		pSuper->body->pLast->pMethod->pVarList->pFirst = pBody->pMethod->pVarList->pFirst;
		pSuper->body->pLast->pMethod->pVarList->pLast = pBody->pMethod->pVarList->pLast;
	} else {
		pSuper->body->pLast->pMethod->pVarList->pFirst = NULL;
		pSuper->body->pLast->pMethod->pVarList->pLast = NULL;
	}
	pSuper->body->pLast->var = NULL;
}
